﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;

namespace wallstreet
{
    public partial class Control_MyStocks_Remove : PhoneApplicationPage
    {
        public Control_MyStocks_Remove()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);

            if (IsolatedStorageSettings.ApplicationSettings.Contains("stockList"))
            {
                List<StockInfo> stockList = IsolatedStorageSettings.ApplicationSettings["stockList"] as List<StockInfo>;
                lboMyStocks.ItemsSource = stockList;
            }
        }

        void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void btnBack_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void lboMyStocks_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            StockInfo selectedStock = lboMyStocks.SelectedItem as StockInfo;

            if (selectedStock != null)
            {
                List<StockInfo> stockList = IsolatedStorageSettings.ApplicationSettings["stockList"] as List<StockInfo>;

                var stock =
                from s in stockList
                orderby s.Company
                where s.Symbol.ToLower() != selectedStock.Symbol.ToLower()
                select s;

                List<StockInfo> list = stock.ToList();
                lboMyStocks.ItemsSource = list;

                if (IsolatedStorageSettings.ApplicationSettings.Contains("stockList"))
                    IsolatedStorageSettings.ApplicationSettings["stockList"] = list;
                else
                    IsolatedStorageSettings.ApplicationSettings.Add("stockList", list);

                if (IsolatedStorageSettings.ApplicationSettings.Contains("stockList_changed"))
                    IsolatedStorageSettings.ApplicationSettings["stockList_changed"] = "true";
                else
                    IsolatedStorageSettings.ApplicationSettings.Add("stockList_changed", "true");

                MessageBox.Show(selectedStock.Company + " was removed from your stocklist");
            }
            //bool blnResult = System.Windows.Browser.HtmlPage.Window.Confirm("Are you sure?");

            //if (blnResult)
            //{
            //}
        }
    }
}